﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class GetStocksForIssuance : RibbonForm
    {
        public GetStocksForIssuance()
        {
            InitializeComponent();
        }
        public string DocumentNumber { get; set; }
        public string ItemCode { get; set; }
        public string ItemName { get; set; }
        public string UoM { get; set; }
        public string InvLotNumber { get; set; }
        public string InvExpireDate { get; set; }
        public string InvMfrDate { get; set; }
        public string Qty { get; set; }
        public string ItemUnitCost { get; set; }
        public string WhsCode { get; set; }
        public string WhsName { get; set; }

        public string TotalServed { get; set; }

        public string ConnectionString { get; set; }

        bool IsNumeric(string _VAR_VALUE)
        {
            try
            {
                int _var = Convert.ToInt16(_VAR_VALUE);

                return true;
            }
            catch
            {
                return false;
            }
        }

      
        private void GetStocksForIssuance_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            txtWarehouse.Text = this.WhsName;

        }


        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            StockManager oManager = new StockManager();
            DataTable _table = new DataTable();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            if (comboBox1.SelectedIndex == 0)
            {
                _table = oManager.BrowseItemCodeAvailableStock(this.WhsCode, txtSearch.Text);
            }
            else
            {
                _table = oManager.BrowseItemNameAvailableStock(this.WhsCode, txtSearch.Text);
            }

            gridControl1.DataSource = _table;
            gridControl1.Refresh();

            oManager.Close();
        }
        private void txtPickQty_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DevExpress.XtraEditors.ButtonEdit oButtonEdit = (DevExpress.XtraEditors.ButtonEdit)sender;

            if (oButtonEdit.Text != "" && IsNumeric(oButtonEdit.Text))
            {
                int iCount = gridView1.FocusedRowHandle;
                

                double _QtySrvd = Convert.ToDouble(oButtonEdit.Text);

                this.ItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemCode"]).ToString();
                this.ItemName = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemName"]).ToString();
                this.UoM = gridView1.GetRowCellValue(iCount, gridView1.Columns["UoM"]).ToString();
                this.InvLotNumber = gridView1.GetRowCellValue(iCount, gridView1.Columns["LotNumber"]).ToString();
                this.Qty = _QtySrvd.ToString();
                this.ItemUnitCost = gridView1.GetRowCellValue(iCount, gridView1.Columns["UnitCost"]).ToString();
                try
                {
                    this.InvExpireDate = Convert.ToString(gridView1.GetRowCellValue(iCount, gridView1.Columns["ExpiryDate"]));
                }
                catch
                {
                    this.InvExpireDate = "";
                }

                try
                {
                    this.InvMfrDate = Convert.ToString(gridView1.GetRowCellValue(iCount, gridView1.Columns["MfrDate"]));
                }
                catch
                {
                    this.InvMfrDate = "";
                }
                this.Close();

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();

            this.ItemCode = "";
        }
        private void txtPickQty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DevExpress.XtraEditors.ButtonEdit oButtonEdit = (DevExpress.XtraEditors.ButtonEdit)sender;

                if (oButtonEdit.Text != "" && IsNumeric(oButtonEdit.Text))
                {
                    int iCount = gridView1.FocusedRowHandle;


                    double _QtySrvd = Convert.ToDouble(oButtonEdit.Text);

                    this.ItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemCode"]).ToString();
                    this.ItemName = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemName"]).ToString();
                    this.UoM = gridView1.GetRowCellValue(iCount, gridView1.Columns["UoM"]).ToString();
                    this.InvLotNumber = gridView1.GetRowCellValue(iCount, gridView1.Columns["LotNumber"]).ToString();
                    this.Qty = _QtySrvd.ToString();
                    this.ItemUnitCost = gridView1.GetRowCellValue(iCount, gridView1.Columns["UnitCost"]).ToString();
                    try
                    {
                        this.InvExpireDate = Convert.ToString(gridView1.GetRowCellValue(iCount, gridView1.Columns["ExpiryDate"]));
                    }
                    catch
                    {
                        this.InvExpireDate = "";
                    }

                    try
                    {
                        this.InvMfrDate = Convert.ToString(gridView1.GetRowCellValue(iCount, gridView1.Columns["MfrDate"]));
                    }
                    catch
                    {
                        this.InvMfrDate = "";
                    }
                    this.Close();

                }
            }
        }
    }
}
